<?php 
session_start();
include("db_connect.php"); 
connect();
$sql="SET CHARACTER SET UTF8";   
query($sql);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php 

if($_POST['action']=="add"){
	if($_POST['font']<>"" and $_POST['font']==$_SESSION['security_code']){
		$data = array(
			"name"=>$_POST['name'],
			"tel"=>$_POST['phone'],
			"email"=>$_POST['email'],
			"province"=>$_POST['province'],
			"car_type"=>$_POST['car_type'],
			"expire"=>$_POST['datepicker'],
			"use_for"=>$_POST['use_car'],
			"brand"=>$_POST['brand_car'],
			"model"=>$_POST['model'],
			"car_year"=>$_POST['year_car'],
			"car_number"=>$_POST['car_id'],
			"old_insure"=>$_POST['company1'],
			"amount_insured"=>$_POST['allcost1'],
			"old_interest"=>$_POST['cost1'],
			"good_profile"=>$_POST['dd'],
			"name_insure"=>$_POST['company2'],
			"name_insure2"=>$_POST['company3'],
			"budget"=>$_POST['allcost2'],
			"budget_more_than"=>$_POST['cost2'],
			"claim_profile"=>$_POST['claim'],
			"claim_ans"=>$_POST['exam_pakan'],
			"insure_type"=>$_POST['driver'],
			"driver_age1"=>$_POST['driver_age1'],
			"driver_age2"=>$_POST['driver_age2'],
			"buy_first"=>$_POST['deduction'],
			"message"=>$_POST['comment'],
			"date_regis"=>date("Y-m-d"),
			"unique_id"=>$_POST['unique_id']
		);
	
		if(insert("tb_firstclass",$data))
		{
			$fileupload=$_FILES['fileField']['tmp_name'];
			$fileupload_name=$_FILES['fileField']['name'];
			$fileupload_size=$_FILES['fileField']['size'];
			$fileupload_type=$_FILES['fileField']['type'];
			if($fileupload)
			{
				$array_last=explode(".",$fileupload_name);
				$c=count($array_last)-1; 
				$lastname=strtolower($array_last[$c]) ;
				$filename=date("YmdHis")."_".mysql_insert_id().".".$lastname;
				if(move_uploaded_file($fileupload,"upload/$filename")){
					$data = array(
						"document"=>$filename
					);
					update("tb_firstclass",$data,"unique_id = '$_POST[unique_id]'");
				}
				
			}
				
			$to_name ="";
			$from_name	="Central Broker";
			$email_user_send ="central.insure.broker@gmail.com";
			$email_pass_send ="showidea";
			$reply_email = $_POST['email'];
			$reply_name = $_POST['name'];
				
			$subject = "ต้องการสอบถามประกันชั้น1 จากคุณ ".$_POST['name'];
			$body_text = "";
			$body_html ="
			ชื่อ :: ".$_POST['name']."
			
			เบอร์โทรติดต่อ :: ".$_POST['phone']."
			     
			Email ::  ".$_POST['email']."	
			
			ข้อความถึงบริษัท :: ".$_POST['comment']."
			
			
			";
				
							  
			$sql="SELECT * FROM tb_admin ";
			$qr = select($sql);
			$i=0;
			while($i<count($qr))
			{
				$rs = $qr[$i];
				$i++;
				$to_email=$rs['email'];
				scriptdd_sendmail($to_name,$to_email,$from_name,$email_user_send,$email_pass_send,$subject,$body_html,$body_text,$reply_email,$reply_name); 
			} 
			echo "<script>alert('บันทึกข้อมูลเรียบร้อยแล้วค่ะ กรุณารอการติดต่อกลับจากเจ้าหน้าที่');window.location='index.php';</script>";
		}
	}else{
		echo "<script>alert('รหัสความปลอดภัยไม่ถูกต้อง');window.location='insure1.php';</script>";
	}
} 
?>
</body>
</html>
